package com.demo.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.entity.Category;
import com.demo.mapper.CategoryMapper;
import com.demo.service.CategoryService;
import com.demo.util.MyPage;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;

/**
 * (Category)表服务实现类
 *
 * @author Sakura
 * @since 2023-11-26 17:11:07
 */
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper,Category> implements CategoryService {
    @Resource
    private CategoryMapper categoryMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Category queryById(Long id) {
        return this.categoryMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param pageNo    当前页
     * @param pageSize  数据大小
     * @param searchKey 查询关键字
     * @return 查询结果
     */
    @Override
    public MyPage<Category> queryByPage(Integer pageNo, Integer pageSize, String searchKey) {
        long total = this.categoryMapper.count(searchKey);
        int start=(pageNo-1)*pageSize;
        return new MyPage<Category>(this.categoryMapper.queryPage(start,pageSize,searchKey),total);
    }

    /**
     * 新增数据
     *
     * @param category 实例对象
     * @return 实例对象
     */
    @Override
    public boolean insert(Category category) {
        int result = this.categoryMapper.insert(category);
        return result > 0;
    }

    /**
     * 修改数据
     *
     * @param category 实例对象
     * @return 实例对象
     */
    @Override
    public boolean updateById(Category category) {
        int result = this.categoryMapper.updateById(category);
        return result > 0;
     }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.categoryMapper.deleteById(id) > 0;
    }
}
